/**
 * Project: TacticaPescaditos
 * File: Attacking.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.modelo.Constantes;
import org.javahispano.javacup.modelo.Posicion;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: Attacking
 */
public class Attacking implements State<TacticaPescaditos> {
	
	
	public static final State<TacticaPescaditos> instance = new Attacking();
	private static final int NUM_REGIONS = 3;
	private static final double THRESHOLD = Constantes.LARGO_CAMPO_JUEGO/(NUM_REGIONS+1);
	private int currentIndex;
	private Posicion[][] homeRegions;
	
	private void initializeHomeRegions() {
		homeRegions = new Posicion[NUM_REGIONS][];
		


		homeRegions[0]=new Posicion[]{

			        new Posicion(0.0,-52.0),
			        new Posicion(7.37062937062937,-12.828054298642533),
			        new Posicion(-27.81818181818182,-12.59049773755656),
			        new Posicion(-10.937062937062937,-13.065610859728507),
			        new Posicion(24.48951048951049,-11.877828054298643),
			        new Posicion(23.776223776223777,11.877828054298643),
			        new Posicion(-2.13986013986014,12.115384615384617),
			        new Posicion(-24.48951048951049,10.690045248868778),
			        new Posicion(-23.3006993006993,31.59502262443439),
			        new Posicion(-2.13986013986014,31.83257918552036),
			        new Posicion(22.11188811188811,31.59502262443439)
			    };

		homeRegions[1]=new Posicion[]{
			        new Posicion(0.0,-52.0),
			        new Posicion(7.37062937062937,-4.276018099547511),
			        new Posicion(-26.391608391608393,-4.276018099547511),
			        new Posicion(-9.986013986013985,-3.3257918552036196),
			        new Posicion(23.776223776223777,-4.038461538461538),
			        new Posicion(15.454545454545453,18.529411764705884),
			        new Posicion(-0.951048951048951,28.98190045248869),
			        new Posicion(-15.692307692307693,17.816742081447966),
			        new Posicion(-23.776223776223777,32.54524886877828),
			        new Posicion(-1.188811188811189,46.56108597285068),
			        new Posicion(19.97202797202797,32.782805429864254)
			    };

		homeRegions[2]=new Posicion[]{
			        new Posicion(0.0,-52.0),
			        new Posicion(6.6573426573426575,20.429864253393667),
			        new Posicion(-25.44055944055944,14.728506787330318),
			        new Posicion(-8.55944055944056,19.479638009049776),
			        new Posicion(22.349650349650346,15.91628959276018),
			        new Posicion(20.20979020979021,34.68325791855204),
			        new Posicion(-1.188811188811189,32.54524886877828),
			        new Posicion(-19.734265734265733,33.257918552036195),
			        new Posicion(-6.6573426573426575,50.124434389140276),
			        new Posicion(-0.4755244755244755,43.23529411764706),
			        new Posicion(4.9930069930069925,50.36199095022624)
			    };


	}

	private Attacking() {
		initializeHomeRegions();
	}

	public void enter(TacticaPescaditos o) {
		currentIndex = calculateCurrentIndex(o);
		o.setHomePositions(homeRegions[currentIndex] );
		
	}
	
	private int calculateCurrentIndex(TacticaPescaditos o){
		double height = o.getBall().getPosition().y + o.getHeightPitch();
		
		int index = (int) Math.ceil( height/THRESHOLD);
		if (index >= homeRegions.length){
			index = homeRegions.length-1;
		} else if (index < 0 ) {
			index=0;
		}
		return index;
	}

	public void execute(TacticaPescaditos o) {
		  if (!o.isControlling()){
			  o.getStateMachine().changeState(Defending.instance);
		  }	else {
				int index = calculateCurrentIndex(o);
				if (currentIndex != index){
					currentIndex = index;
					o.setHomePositions(homeRegions[currentIndex] );
				}
				
				o.determineBestSupportingPosition();
			}
		
	}

	public void exit(TacticaPescaditos o) {
		
	}

	public boolean onMessage(TacticaPescaditos o, Telegram telegram) {
		// TODO Auto-generated method stub
		return false;
	}

}
